//
//  LVBaseTableViewVC.m
//  ListViewModel
//
//  Created by 曹连华 on 2022/7/14.
//

#import "LVBaseTableViewVC.h"
#import "LVBaseSectionHeaderFooterView.h"

#define WEAK_SELF __weak typeof(self)weakSelf = self;
#define STRONG_SELF __strong typeof(weakSelf)self = weakSelf;

@interface LVBaseTableViewVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation LVBaseTableViewVC

- (LVBaseTableViewModel *)viewModel {
    if(!_viewModel) {
        _viewModel = [[LVBaseTableViewModel alloc]init];
    }
    return _viewModel;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:UIScreen.mainScreen.bounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self.viewModel.dataSource;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
//        [_tableView registerClass:LVBaseSectionHeaderFooterView.class forHeaderFooterViewReuseIdentifier:@"LVBaseSectionHeaderView"];
//        [_tableView registerClass:LVBaseSectionHeaderFooterView.class forHeaderFooterViewReuseIdentifier:@"LVBaseSectionFooterView"];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.viewModel.dataSource resetDataSource:self];
    
    [self addTableView];
    
    [self addReloadTableViewBlock];
    
    [self actionsBlock];
}

- (void)addTableView {
    self.view.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:self.tableView];
}
- (void)addReloadTableViewBlock {
    WEAK_SELF
    self.viewModel.reloadTableViewBlock = ^(NSIndexSet * _Nullable sections, NSArray<NSIndexPath *> * _Nullable indexPaths) {
        STRONG_SELF
        if (sections) {
            [self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationNone];
        } else if(indexPaths) {
            [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
        } else {
            [self.tableView reloadData];
        }
    };
}
- (void)actionsBlock {
    
    self.viewModel.dataSource.removeRowModelBlock = ^(RowModelEditOperateResultBlock  _Nullable operateResultBlock, NSIndexPath * _Nullable indexPath) {
        operateResultBlock(YES);
    };
    
    self.viewModel.dataSource.insertRowModelBlock = ^(RowModelEditOperateResultBlock  _Nullable operateResultBlock, NSIndexPath * _Nullable indexPath) {
        operateResultBlock(YES);
    };
    
    self.viewModel.dataSource.moveRowModelBlock = ^(RowModelEditOperateResultBlock  _Nullable operateResultBlock, NSIndexPath * _Nullable sourceIndexPath, NSIndexPath * _Nullable destinationIndexPath) {
        operateResultBlock(YES);
    };
    
    self.viewModel.showLoadingBlock = ^(NSString * _Nullable message) {};
    self.viewModel.hideLoadingBlock = ^(NSString * _Nullable message) {};
    self.viewModel.makeToastBlock = ^(NSString * _Nullable message) {};
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.viewModel didSelectedRowModelWithIndexPath:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self.viewModel.dataSource rowModelAtIndex:indexPath].rowHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    LVBaseCellSectionModel *sectionModel = [self.viewModel.dataSource sectionModelAtIndex:section];
    return sectionModel.sectionHeader.size.height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    LVBaseCellSectionModel *sectionModel = [self.viewModel.dataSource sectionModelAtIndex:section];
    return sectionModel.sectionFooter.size.height;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    LVBaseCellSectionModel *sectionModel = [self.viewModel.dataSource sectionModelAtIndex:section];
    
    LVBaseSectionHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:sectionModel.sectionHeader.reuseIdentifier];
    
    [headerView updateWithHeaderFooterModel:sectionModel.sectionHeader atSection:section];
    return headerView;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    LVBaseCellSectionModel *sectionModel = [self.viewModel.dataSource sectionModelAtIndex:section];
    LVBaseSectionHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:sectionModel.sectionFooter.reuseIdentifier];
    [footerView updateWithHeaderFooterModel:sectionModel.sectionFooter atSection:section];
    return footerView;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self.viewModel.dataSource rowModelAtIndex:indexPath].editingStyle;
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self.viewModel.dataSource rowModelAtIndex:indexPath].titleForEdit;
}
@end
